﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using m_pricelist._BAL_;
using m_pricelist.Proposal_Services;
using m_pricelist.Services;
using Excel = Microsoft.Office.Interop.Excel;
using Word = Microsoft.Office.Interop.Word;

namespace m_pricelist
{
    public partial class Proposal : Form
    {
        #region Member variables
        //Til að skera úr um hvort sé verið að nota b_pricelist eða b_project
        public bool b_pricelist { get; set; }
        public bool b_project { get; set; }

        //Excel tilvik sem eru notuð til að ná sambandi við Excel. Þetta er new-að þar sem þetta er notað
        public Excel.Application xlApp = (Excel.Application)Marshal.GetActiveObject("Excel.Application");
        public Excel.Workbook xlWorkBook = Globals.ThisAddIn.Application.ActiveWorkbook;
        public Excel.Workbooks xlWorkBookObjects = null;
        public Excel.Worksheet xlSheet = null;

        public Word.Application wdApp { get; set; }

        NewProposal_BAL newProposal;

        //Arraylistar sem eru notaðir til að halda utan um þau element sem eru í listboxunum á Appendix tabinu í forminu
        public ArrayList lsbArrayList = new ArrayList();
        public ArrayList lsbAppNameList = new ArrayList();
        //Þessi arraylisti heldur utan um þau stök sem eru í ShortName dálkunum í Appendix töflunni í gagnagrunni
        public ArrayList lsbShortNameList = new ArrayList();

        //Þessir listar gera það sama. Listarnir geta stækkað og minnkað.
        List<Type_Appendix> businessObjects = new List<Type_Appendix>();

        #endregion

        #region constructor
        public Proposal() // Smiður sem keyrist sjálfkrafa þegar Formið er ræst
        {
            InitializeComponent(); // Hluti af upphasfstillinarferlinu - Við komum ekki nálægt þessu
            this.BackColor = Color.FromArgb(0, 44, 83); //Til að fá réttan lit; á formið
        }
        #endregion

        #region Private og protected föll
        private void Proposal_Load(object sender, EventArgs e)
        {
            Proposal proposal = null;
            PricelistProposal pricelist = new PricelistProposal();
            //Hér eru Excel klasarnir new-aðir             
            xlWorkBookObjects = (Excel.Workbooks)xlApp.Workbooks;
            xlSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            ReadDBappendix(); //Kalla á ReadDBappendix fallið 

            string Project_Sheet_name = "Main Overview", Pricelist_Sheet_Name = "PriceSummary", Default_Sheet_Name = "Sheet1";
            if (xlSheet.Name.Equals(Project_Sheet_name))
            {
                //Ef C/D project skjalið er opið þá keyrist þetta fall til að upphafstilla formið
                b_project = true;
            }
            else if (xlSheet.Name.Equals(Pricelist_Sheet_Name) || xlSheet.Name.Equals("Output"))
            {
                //Ef Pricelist skjalið er opið þá keyrist þetta fall til að upphafstilla formið
                b_pricelist = true;
                pricelist.UserForm_Initialize_PriceList(proposal,  b_pricelist, ref xlSheet);
            }
            else if (xlSheet.Name.Equals(Default_Sheet_Name))
            {
                MessageBox.Show("Please open one of the templates, Pricelist or C/D Projects and try again ");
                //Hreinsa upp og fara aftur á byrjunarreit
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();

                Marshal.FinalReleaseComObject(xlWorkBookObjects);
                Marshal.FinalReleaseComObject(xlSheet);
                cmdClose_Click(this, EventArgs.Empty);
            }  
        }
            #endregion

        #region New Proposal föll
        #region cmdNewPro
        private void cmdNewPro_Click(object sender, EventArgs e)
        {
            if (optNewOneLang.Checked)
            {
                //newProposal.CmdPro();
            }

            else if (optNewTwoLang.Checked)
            {
                //newProposal.CmdPro();

            }
            else if (optNewSLA.Checked)
            {
                //newProposal.CmdPro();

            }
        }
        #endregion

        #region cmdNproposal
        private void cmdNproposal_Click_1(object sender, EventArgs e)
        {
            newProposal = new NewProposal_BAL();
            Proposal proposal = null;
            bNewProposal bNew = null;
            if (optNewOneLang.Checked)
            {
                newProposal.cmdProposal(proposal, bNew, ref xlSheet);
            }
            else if (optNewTwoLang.Checked)
            {
                //newProposal.cmdProposal();
            }
            else if (optNewSLA.Checked)
            {
                //newProposal.cmdProposal();
            }

        }
        #endregion

        #region cmdUProposal
        protected void cmdUproposal_Click(object sender, EventArgs e)
        {
            if (optNewOneLang.Checked)
            {
                //newProposal.cmdUproposal();
            }
            else if (optNewTwoLang.Checked)
            {
                //newProposal.cmdUproposal();
            }
            else if (optNewSLA.Checked)
            {
                //newProposal.cmdUproposal();
            }
        }
        #endregion

        #endregion

        #region Old Proposal föll
        #endregion

        #region Sameiginleg föll
        #region Close
        //Close takkinn á forminu
        public void cmdClose_Click(object sender, EventArgs e)
        {
            //Þessi föll sjá um að skila til baka objectum og minni sem hafa verið tekin frá
            //Las á netinu að gott væri að kalla á þetta tvisvar. Ef maður vill vera alveg viss að skila öllu til baka
            //Þá þarf að gera það manually Dæmi: Marshal.FinalReleaseComObject(xlRng); Marshal.FinalReleaseComObject(xlSheet);
            GC.Collect();
            GC.WaitForPendingFinalizers();

            GC.Collect();
            GC.WaitForPendingFinalizers();

            this.Close();
        }


        #endregion


        #endregion

        #region SeaTransportOnly CheckedBox
        private void Initilize_SeaTransport()
        {
            //til að keyra upp SeaTransportOnly layoutið
            optFob.Visible = false;
            optCFR.Visible = false;
            optCIF.Visible = false;
            optFCA.Visible = true;
            optCPT.Visible = true;
            optCIP.Visible = true;
            optDAP.Visible = true;
            optDAT.Visible = true;
        }
        #endregion

        #region CheckChanged takkar New og Old Proposal

        #region New Proposal
        #region Proposal svæði
        public void optNewOneLang_CheckedChanged(object sender, EventArgs e)
        {
            cmdNewPro.Top = 26;
            chkNewApp3.Enabled = true;
            chkNewApp3.Top = 54;
            optNewTwoLang.Top = 80;
        }

        public void optNewTwoLang_CheckedChanged(object sender, EventArgs e)
        {
            cmdNewPro.Top = 54;
            chkNewApp3.Enabled = true;
            chkNewApp3.Top = 86;
            optNewTwoLang.Top = 60;
        }
        public void optNewSLA_CheckedChanged(object sender, EventArgs e)
        {
            cmdNewPro.Top = 134;
            chkNewApp3.Enabled = false;
        }
        #endregion

        #region Delivery Terms svæði
        public void chkNewSeaOnly_CheckedChanged(object sender, EventArgs e)
        {
            if (chkNewSeaOnly.Checked)
            {
                optNewFob.Visible = true;
                optNewCFR.Visible = true;
                optNewCIF.Visible = true;
                optNewFCA.Visible = false;
                optNewCPT.Visible = false;
                optNewCIP.Visible = false;
                optNewDAP.Visible = false;
                optNewDAT.Visible = false;
                txtFinalAdress.Top = 50;
                txtFinalAdress.Left = 90;
            }
            else
            {
                optNewFob.Visible = false;
                optNewCFR.Visible = false;
                optNewCIF.Visible = false;
                optNewFCA.Visible = true;
                optNewCPT.Visible = true;
                optNewCIP.Visible = true;
                optNewDAP.Visible = true;
                optNewDAT.Visible = true;
                txtFinalAdress.Top = 50;
                txtFinalAdress.Left = 140;
            }
        }

        public void optNewFob_Changed_Custom(string m_sFirstPort)
        {
            optNewFob.Checked = true;
            txtNewFinalAdress.Top = 36;
            chkNewSeaOnly.Checked = true;
            txtNewFinalAdress.Text = m_sFirstPort;
        }

        public void optNewCFR_Changed_Custom(string m_sFinaldest, string m_sFirstPort)
        {
            optNewCFR.Checked = true;
            txtFinalAdress.Top = 58;
            chkNewSeaOnly.Checked = true;
            txtNewFinalAdress.Text = (m_sFinaldest == "") ? m_sFirstPort : m_sFinaldest;
        }

        public void optNewCIF_Changed_Custom(string m_sFinaldest, string m_sFirstPort)
        {
            optNewCIF.Checked = true;
            txtNewFinalAdress.Top = 81;
            chkNewSeaOnly.Checked = true;
            txtNewFinalAdress.Text = (m_sFinaldest == "") ? m_sFirstPort : m_sFinaldest;
        }

        public void optNewFCA_Changed_Custom(string m_sFirstPort)
        {
            optNewFCA.Checked = true;
            txtNewFinalAdress.Top = 36;
            txtNewFinalAdress.Text = m_sFirstPort;
        }

        public void optNewCPT_Changed_Custom(string m_sFinaldest)
        {
            optNewCPT.Checked = true;
            txtNewFinalAdress.Top = 58;
            txtNewFinalAdress.Text = m_sFinaldest;
        }

        public void optNewCIP_Changed_Custom(string m_sFinaldest)
        {
            optNewCIP.Checked = true;
            txtNewFinalAdress.Top = 81;
            txtNewFinalAdress.Text = m_sFinaldest;
        }

        public void optNewDAP_Changed_Custom(string m_sFinaldest)
        {
            optNewDAP.Checked = true;
            txtNewFinalAdress.Top = 102;
            txtNewFinalAdress.Text = m_sFinaldest;
        }

        public void optNewDAT_Changed_Custom(string m_sFinaldest)
        {
            optNewDAT.Checked = true;
            txtNewFinalAdress.Top = 122;
            txtNewFinalAdress.Text = m_sFinaldest;
        }

        #endregion

        #region Installation, startup
        public void chkNewHardware_CheckedChanged(object sender, EventArgs e)
        {
            chkNewHardware.Checked = true;
        }

        public void chkNewTHardware_CheckedChanged(object sender, EventArgs e)
        {
            chkNewTHardware.Checked = true;
        }

        public void chkNewSoftware_CheckedChanged(object sender, EventArgs e)
        {
            chkNewSoftware.Checked = true;
        }

        public void chkNewTSoftware_CheckedChanged(object sender, EventArgs e)
        {
            chkNewTSoftware.Checked = true;
        }

        #endregion

        #endregion

        #region Old Proposal
        #region Proposal svæði
        public void optOneLang_CheckedChanged(object sender, EventArgs e)
        {
            cmdPro.Top = 26;
            chkApp3.Enabled = true;
            chkApp3.Top = 54;
            optTwoLang.Top = 80;
        }

        public void optTwoLang_CheckedChanged(object sender, EventArgs e)
        {
            cmdPro.Top = 54;
            chkApp3.Enabled = true;
            chkApp3.Top = 86;
            optTwoLang.Top = 60;
        }

        public void optSLA_CheckedChanged(object sender, EventArgs e)
        {
            cmdPro.Top = 134;
            chkApp3.Enabled = false;
        }

        public void chkApp3_CheckedChanged(object sender, EventArgs e)
        {
            chkApp3.Checked = true;
            chkApp3.Enabled = false;
        }


        #endregion

        #region Delivery Terms svæði
        public void chkSeaOnly_CheckedChanged(object sender, EventArgs e)
        {
            //Ef chkSeaOnly er virkt
            if (chkSeaOnly.Checked)
            {
                optFob.Visible = true;
                optCFR.Visible = true;
                optCIF.Visible = true;
                optFCA.Visible = false;
                optCPT.Visible = false;
                optCIP.Visible = false;
                optDAP.Visible = false;
                optDAT.Visible = false;
                txtFinalAdress.Top = 50;
                txtFinalAdress.Left = 90;
            }
            //Annars
            else
            {
                optFob.Visible = false;
                optCFR.Visible = false;
                optCIF.Visible = false;
                optFCA.Visible = true;
                optCPT.Visible = true;
                optCIP.Visible = true;
                optDAP.Visible = true;
                optDAT.Visible = true;
                txtFinalAdress.Top = 50;
                txtFinalAdress.Left = 140;
            }
        }

        public void optFCA_CheckedChanged(object sender, EventArgs e, string m_sFirstPort)
        {
            optFCA.Checked = true;
            txtFinalAdress.Top = 36;
            txtFinalAdress.Text = m_sFirstPort;
        }

        public void optCPT_CheckedChanged(object sender, EventArgs e, string m_sFinaldest)
        {
            optCPT.Checked = true;
            txtFinalAdress.Top = 58;
            txtFinalAdress.Text = m_sFinaldest;
        }

        public void optCIP_CheckedChanged(object sender, EventArgs e, string m_sFinaldest)
        {
            optCIP.Checked = true;
            txtFinalAdress.Top = 81;
            txtFinalAdress.Text = m_sFinaldest;
        }

        public void optDAP_CheckedChanged(object sender, EventArgs e, string m_sFinaldest)
        {
            optDAP.Checked = true;
            txtFinalAdress.Top = 102;
            txtFinalAdress.Text = m_sFinaldest;
        }

        public void optDAT_CheckedChanged(object sender, EventArgs e, string m_sFinaldest)
        {
            optDAT.Checked = true;
            txtFinalAdress.Top = 122;
            txtFinalAdress.Text = m_sFinaldest;
        }

        public void optFob_CheckedChanged(object sender, EventArgs e, string m_sFirstPort)
        {
            optFob.Checked = true;
            txtFinalAdress.Top = 36;
            chkSeaOnly.Checked = true;
            txtFinalAdress.Text = m_sFirstPort;
        }

        public void optCFR_CheckedChanged(object sender, EventArgs e, string m_sFinaldest, string m_sFirstPort)
        {
            optCFR.Checked = true;
            txtFinalAdress.Top = 58;
            chkSeaOnly.Checked = true;
            txtFinalAdress.Text = (string.IsNullOrEmpty(m_sFinaldest) ? m_sFirstPort : m_sFinaldest);
        }

        public void optCIF_CheckedChanged(object sender, EventArgs e, string m_sFinaldest, string m_sFirstPort)
        {
            optCIF.Checked = true;
            txtFinalAdress.Top = 81;
            chkSeaOnly.Checked = true;
            txtFinalAdress.Text = (string.IsNullOrEmpty(m_sFinaldest) ? m_sFirstPort : m_sFinaldest);
        }
        #endregion

        #region Installation, startup
        public void chkHardware_CheckedChanged(object sender, EventArgs e)
        {
            chkHardware.Checked = true;
        }

        public void chkSoftware_CheckedChanged(object sender, EventArgs e)
        {
            chkSoftware.Checked = true;
        }

        public void chkTHardware_CheckedChanged(object sender, EventArgs e)
        {
            chkTHardware.Checked = true;
        }

        public void chkTSoftware_CheckedChanged(object sender, EventArgs e)
        {
            chkTSoftware.Checked = true;
        }

        public string txtFinalAdress_TextChanged()
        {
            string temp = txtFinalAdress.Text;
            return temp;
        }
        #endregion

        #region Payment Terms
        public void optStandard_CheckedChanged(object sender, EventArgs e)
        {
            optStandard.Checked = true;
        }

        public void optLC_CheckedChanged(object sender, EventArgs e)
        {
            optLC.Checked = true;
        }

        public void optLC100_CheckedChanged(object sender, EventArgs e)
        {
            optLC100.Checked = true;
        }

        public void optPrepaid_CheckedChanged(object sender, EventArgs e)
        {
            optPrepaid.Checked = true;
        }

        public void optOption1_CheckedChanged(object sender, EventArgs e)
        {
            optOption1.Checked = true;
        }

        public void optOption2_CheckedChanged(object sender, EventArgs e)
        {
            optOption2.Checked = true;
        }
        #endregion

        #endregion
        #endregion

        #region Appendix
        public void ReadDBappendix()
        {
            //Gögn úr töflunni Appendix View eru sett í DataSet.
            try
            {
                Excel.Range rFundin = null;//Í VB kóðanum er kallað í rFundin, sem er kommentað út hér að neðan, en ég sé ekki alveg ástæðu til þess 
                //að halda því inni. 01.02.2013 ÞE

                Products pr = new Products();
                Type_Appendix tp = new Type_Appendix();

                int a = 0;
                string b = "", c = "", d = "";
                string sSup = "";

                lsbAppIn.Items.Clear();
                lsbAppOut.Items.Clear();
                cboSupplier.Items.Clear();

                ////Svo er farið í gegnum arraylistann og hann settur í lista sem heitir businessObjects.
                foreach (Object objRow in pr.getB_Appendix().Tables[0].Rows)
                {
                    a = (Convert.ToInt32(((DataRow)objRow)["Id"]));
                    b = (((DataRow)objRow)["Short_Name"].ToString());
                    c = (((DataRow)objRow)["Appname"].ToString());
                    d = (((DataRow)objRow)["Supplier_Name"].ToString());
                    businessObjects.Add(new Type_Appendix(a, b, c, d));
                }

                //Hér er foreach lykkja sem fer í gegnum öll objection í businessObjects og þeim bætt í Combolistann
                //Athugað er hvort Supplier_Name hafi komið fyrir áður, ef ekki þá er því bætt í listann, annars ekki.
                foreach (var objRow in businessObjects)
                {
                    if (sSup != objRow.sSupplierName)
                    {
                        sSup = objRow.sSupplierName;
                        cboSupplier.Items.Add(sSup);
                    }
                }

                //Hér verða listboxinn fyllt.
                foreach (var objRow1 in businessObjects)
                {
                    lsbAppOut.Items.Add(objRow1.lId + " " + objRow1.sShortName);
                }

            }
            catch (Exception ex)
            {
                //ps.CleanUp(string.Empty);
                System.Windows.Forms.MessageBox.Show(ex.ToString());
            }

        }

        #region cboSupplier
       
        private void cboSupplier_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                lsbAppIn.Items.Clear();
                lsbAppOut.Items.Clear();
                //cboSupplier.Items.Clear();
                string sSup = null;
                //Hreinsum listann og náum bara í það sem við viljum sjá
                //sSup heldur utan hvaða index er valinn
                sSup = cboSupplier.Text;

                //Loopum í gegnum lsbArrayListann og finnum það sem tilheyrir þeim index sem er valinn
                foreach (var objRow in businessObjects)
                {
                    if (sSup == objRow.sSupplierName)
                    {
                        lsbAppOut.Items.Add(objRow.lId + " " + objRow.sShortName);
                    }
                }
            }
            catch (Exception ex)
            {
                //ps.CleanUp(string.Empty, null);
                MessageBox.Show(ex.ToString());
            }
        }
        #endregion
        #region cmdAppin
        //cmdAppIn - Er takki til að flytja strengi á milli listboxanna
        protected void cmdAppIn_Click(object sender, EventArgs e)
        {
            if (lsbAppOut.SelectedIndex != -1)
            {
                foreach (var a in businessObjects)
                {
                    if (lsbAppOut.SelectedItem.Equals(a.lId + " " + a.sShortName))
                    {
                        lsbAppNameList.Add(a.sAppName);
                        lsbShortNameList.Add(a.sShortName);
                    }
                }
                lsbAppIn.Items.Add(lsbAppOut.SelectedItem);
                lsbAppOut.Items.Remove(lsbAppOut.SelectedItem);
            }
        }
        #endregion
        #region cmdAppOut
        //cmdAppOut - Er takki til að flytja strengi á milli listboxanna
        protected void cmdAppOut_Click(object sender, EventArgs e)
        {
            if (lsbAppIn.SelectedIndex != -1)
            {
                lsbAppOut.Items.Add(lsbAppIn.SelectedItem);
                lsbAppIn.Items.Remove(lsbAppIn.SelectedItem);
            }
        }
        #endregion

    
        
        #endregion
    }
}
